package com.example.sophia

import android.annotation.SuppressLint
import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class MainActivity : AppCompatActivity() {
    private lateinit var messageEditText: EditText

    private lateinit var recyclerView: RecyclerView
    private lateinit var rootView: RelativeLayout

    private val chatMessages = mutableListOf<ChatMessage>()
    private lateinit var adapter: ChatAdapter
    private lateinit var contentView: View

    @SuppressLint("UseSupportActionBar")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

//        toobar
        val toolbar = findViewById<androidx.appcompat.widget.Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

        messageEditText = findViewById(R.id.messageEditText)

        recyclerView = findViewById(R.id.recyclerView)
        contentView = findViewById<View>(android.R.id.content)
        contentView.viewTreeObserver.addOnGlobalLayoutListener {
            val r = Rect()
            contentView.getWindowVisibleDisplayFrame(r)
            val screenHeight = contentView.rootView.height
            val keypadHeight = screenHeight - r.bottom

            if (keypadHeight > screenHeight * 0.15) {
                val location = IntArray(2)
                messageEditText.getLocationInWindow(location)
                val editTextBottom = location[1] + messageEditText.height
                if (editTextBottom > screenHeight - keypadHeight) {
                    contentView.setPadding(0, 0, 0, keypadHeight)
                }
            } else {
                contentView.setPadding(0, 0, 0, 0)
            }
        }

        adapter = ChatAdapter(chatMessages)

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter

        val sendButton = findViewById<Button>(R.id.sendButton);
        sendButton.setOnClickListener{
            val messageText = messageEditText.text.toString()
            if (messageText.isNotEmpty()) {
                chatMessages.add(ChatMessage(messageText, false))
                adapter.notifyItemInserted(chatMessages.size - 1)
                chatMessages.add(ChatMessage("rev", true))
                recyclerView.scrollToPosition(chatMessages.size - 1)
                messageEditText.setText("")
            } else {
                Toast.makeText(this, "请输入消息", Toast.LENGTH_SHORT).show()
                messageEditText.setText(stringFromJNI())
                messageEditText.setText("")
            }
        }
    }

    /**
     * A native method that is implemented by the 'sophia' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    companion object {
        // Used to load the 'sophia' library on application startup.
        init {
            System.loadLibrary("sophia")
        }
    }
}